package com.example.administrator.baikangxing.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.WindowManager;

public class ScreenUtils {
	private static TypedValue mTmpValue = new TypedValue();

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, int dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, int pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 将px值转换为sp值，保证文字大小不变
	 * 
	 * @param pxValue
	 * @param fontScale
	 *            （DisplayMetrics类中属性scaledDensity）
	 * @return
	 */
	public static int px2sp(Context context, float pxValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / fontScale + 0.5f);
	}

	/**
	 * 将sp值转换为px值，保证文字大小不变
	 * 
	 * @param spValue
	 * @param fontScale
	 *            （DisplayMetrics类中属性scaledDensity）
	 * @return
	 */
	public static int sp2px(Context context, float spValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}
	
	/**
	 * 根据资源id获取控件在xml文件中定义的尺寸值(dip,sp,...)
	 * @param context
	 * @param id
	 * @return
	 */
	public static int getXmlDef(Context context, int id) {
		synchronized (mTmpValue) {
			TypedValue value = mTmpValue;
			context.getResources().getValue(id, value, true);
			return (int) TypedValue.complexToFloat(value.data);
		}
	}
	
	/**
	 * 等比例缩放宽高
	 * @param srcWidth 原始宽度
	 * @param srcHeight 原始高度
	 * @param targetMeasure 目标尺寸
	 * @param measure 尺寸类型，0:表示宽度；1:表示高度
	 * @return 缩放后的宽高值
	 */
	public static int[] adjustEqualityConstrain(int srcWidth,int srcHeight,int targetMeasure,int measure){
		int w = 0,h = 0;
		switch(measure){
		case 0:
			w = targetMeasure;
			h = w*srcHeight/srcWidth;
			break;
		case 1:
			h = targetMeasure;
			w = h*srcWidth/srcHeight;
			break;
		}
		return new int[]{w,h};
	}

	/**
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context)
	{
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		@SuppressWarnings("deprecation") int screenWidth = windowManager.getDefaultDisplay().getWidth();
		return screenWidth;
	}

	/**
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context)
	{
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		@SuppressWarnings("deprecation") int screenHeiht = windowManager.getDefaultDisplay().getHeight();
		return screenHeiht;
	}

    /**
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context)
    {

        int statusHeight = -1;
        try
        {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return statusHeight;
    }

}
